<?php

namespace app\components;


use app\exception\ErrorCode;
use app\exception\OaException;
use yii\validators\Validator;

class ActTimeValidator extends Validator
{

    public function validateAttribute($model, $attribute)
    {
        $value = $model->$attribute;
        if (!is_array($value)) throw new OaException(ErrorCode::PARMA_ERROR);


        $arr = [];
        foreach ($value as $r) {
            if (empty($r["beginTime"]) || empty($r["endTime"])) {
                throw new OaException(ErrorCode::PARMA_ERROR);
            }


            $bTime = strtotime($r["beginTime"]);
            $eTime = strtotime($r["endTime"]);
            $key = date("Y-m-d",$bTime);
            if(array_key_exists($key,$arr)) {
                throw new OaException(ErrorCode::PARMA_ERROR);
            }

            if($bTime>$eTime || date("Y-m-d",$bTime) != date("Y-m-d",$eTime)) {
                throw new OaException(ErrorCode::PARMA_ERROR);
            }
            $arr[$key] = 1;
        }




    }
}